package com.fh.shop.api.exception;

import com.fh.shop.api.common.ResponseEnum;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.config.IdempotenceException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 *  统一异常处理器
 *
 *  它会捕获指定的异常
 */

@RestControllerAdvice
public class WebExceptionHandler {

    @ExceptionHandler(TokenException.class)
    public ServerResponse handlerTokenException(TokenException e){
        ResponseEnum responseEnum = e.getResponseEnum();
        return ServerResponse.error(responseEnum);
    }

    @ExceptionHandler(StockException.class)
    public ServerResponse handlerStockException(StockException e){
        ResponseEnum responseEnum = e.getResponseEnum();
        String params = e.getParams();
        return ServerResponse.error(responseEnum,params);
    }

    @ExceptionHandler(IdempotenceException.class)
    public ServerResponse handlerIdempotenceException(IdempotenceException e){
        ResponseEnum responseEnum = e.getResponseEnum();
        return ServerResponse.error(responseEnum);
    }

    @ExceptionHandler(PayException.class)
    public ServerResponse handlerPayException(PayException e){
        ResponseEnum responseEnum = e.getResponseEnum();
        //return "错误："+responseEnum+":"+responseEnum.getMsg();
        return ServerResponse.error(responseEnum);
    }

}
